<?php

namespace App\Http\Controllers;

use App\Http\Requests\EditAdminAuthInput;
use App\Models\AdminAuth;
use App\Models\Auth;
use Login;
use Yo;

class AdminAuthController extends Controller
{
    public function check()
    {
        $id = request()->post('id');
        Login::admin($id);
        return Yo::echo();
    }

    public function menu()
    {
        Login::admin(3);
        $menu_group = Auth::select('id', 'name', 'title', 'icon', 'status')
            ->where('type', 1)->where('show', 1)->where('del', 2)
            ->orderBy('order', 'desc')->get();
        $list = [];
        foreach ($menu_group as $item) {
            switch (Login::$info->admin_auth_id) {
                case -1:
                    $auth_list = Auth::select('id', 'name', 'title', 'icon', 'status')->where('pid', $item->id)
                        ->where('type', 2)->where('show', 1)->where('del', 2)
                        ->orderBy('order', 'desc')->get();
                    break;
                case 0:
                    $auth_list = Auth::select('id', 'name', 'title', 'icon', 'status')->where('pid', $item->id)
                        ->where('type', 2)->where('check_type', 1)->where('show', 1)->where('del', 2)
                        ->orderBy('order', 'desc')->get();
                    break;
                default:
                    $admin_auth = AdminAuth::find(Login::$info->admin_auth_id);
                    $auth_ids = json_decode($admin_auth->auth_ids, true);
                    $auth_list = Auth::select('id', 'name', 'title', 'icon', 'status')
                        ->where(function ($query) use ($auth_ids, $item) {
                            $query->whereIn('id', $auth_ids)->where('pid', $item->id)->where('type', 2)->where('check_type', 2)->where('show', 1)->where('del', 2);
                        })
                        ->orWhere(function ($query) use ($auth_ids, $item) {
                            $query->where('type', 2)->where('pid', $item->id)->where('check_type', 1)->where('show', 1)->where('del', 2);
                        })
                        ->orderBy('order', 'desc')->get();
            }
            if (count($auth_list) !== 0) $list[] = [
                "id" => $item->id,
                "name" => $item->name,
                "title" => $item->title,
                "icon" => $item->icon,
                "status" => $item->status,
                "children" => $auth_list
            ];
        }
        return Yo::echo([
            'list' => $list
        ]);
    }

    public function all()
    {
        Login::admin(6);
        $admin_auth_list = AdminAuth::select('id', 'name', 'del')->orderBy('updated_at', 'desc')->get();
        return Yo::echo([
            'list' => $admin_auth_list
        ]);
    }

    public function list()
    {
        Login::admin(5);
        $admin_auth_list = AdminAuth::select('id', 'name', 'auth_ids', 'remark')->where('del', 2)->orderBy('updated_at', 'desc')->get();
        $list = [];
        foreach ($admin_auth_list as $item) {
            $auth_ids_turn = [];
            foreach (json_decode($item->auth_ids, true) as $i) {
                $auth_ids_turn[] = intval($i);
            }
            $list[] = [
                'id' => $item->id,
                'name' => $item->name,
                'auth_ids' => $item->auth_ids,
                'auth_ids_turn' => $auth_ids_turn,
                'remark' => $item->remark,
            ];
        }
        return Yo::echo([
            'list' => $list
        ]);
    }

    public function delete()
    {
        Login::admin(5);
        $ids = request()->post('ids');
        AdminAuth::whereIn('id', $ids)->update([
            'del' => 1
        ]);
        return Yo::delete_echo($ids);
    }

    public function update(EditAdminAuthInput $request)
    {
        Login::admin(5);
        $id = request()->post('id');
        $name = $request->post('name');
        $auth_ids = $request->post('auth_ids');
        $remark = $request->post('remark');
        $auth_ids_arr = [];
        foreach ($auth_ids as $auth_id) $auth_ids_arr[] = (string)$auth_id;
        $auth_ids_str = json_encode($auth_ids_arr, JSON_UNESCAPED_UNICODE);
        if (mb_strlen($auth_ids_str) > 1000) Yo::error_echo(100007);
        $admin_auth = AdminAuth::find($id);
        if (!$admin_auth) Yo::error_echo(100008);
        if ($admin_auth->del !== 2) Yo::error_echo(100008);
        $admin_auth->name = $name;
        $admin_auth->auth_ids = $auth_ids_str;
        $admin_auth->remark = $remark ?? '';
        $admin_auth->save();
        return Yo::update_echo($admin_auth->id);
    }

    public function create(EditAdminAuthInput $request)
    {
        Login::admin(5);
        $name = $request->post('name');
        $auth_ids = $request->post('auth_ids');
        $remark = $request->post('remark');
        $auth_ids_arr = [];
        foreach ($auth_ids as $auth_id) $auth_ids_arr[] = (string)$auth_id;
        $auth_ids_str = json_encode($auth_ids_arr, JSON_UNESCAPED_UNICODE);
        if (mb_strlen($auth_ids_str) > 1000) Yo::error_echo(100007);
        $admin_auth = AdminAuth::create([
            'name' => $name,
            'auth_ids' => $auth_ids_str ?? '[]',
            'remark' => $remark ?? '',
        ]);
        return Yo::create_echo($admin_auth->id);
    }
}
